package edu.gatech.cs2340.sharks.fms;

/**
 * ManageTagsActivity.java
 * @author Miri Baker
 * 
 * Allows admins to add and remove tags
 */

import edu.gatech.cs2340.sharks.fms.data.*;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;

public class ManageTagsActivity extends ListActivity
{	
	/**
	 * The list of tags
	 */
	private ListView listView;
	
	/**
	 * An array containing the tags to be displayed
	 */
	private Tag[] taglist;
	
	/**
	 * Adapter allowing the tags to be put into the list
	 */
	private TagAdapter tagAdapter;
	
	/**
	 * The current instance of the activity, used to make sure there is only one instance of this activity running at a time.
	 */
	private static Activity instance = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		//Checks to be sure the instance is correct.
		if (instance != null) instance.finish();
		instance = this;
		
		Packet<List<Tag>> tags = DBMgr.allTags();

		/* If the packet is OK to use, adds each tag in the list
		 * to a new list and sets the fields of the tag's listview
		 * appropriately.
		 * If packet is not OK, throws a dialog box with status code.
		 */
		if(tags.status.code() == Status.Code.OK)
		{
			taglist = new Tag[tags.value.size()];
			for (int i = 0; i < tags.value.size(); i++) taglist[i] = tags.value.get(i);
			listView = getListView();
			tagAdapter = new TagAdapter(this, R.layout.listview_tags_row, taglist);
			View header = (View)getLayoutInflater().inflate(R.layout.tags_header, null);
			listView.addHeaderView(header, null, false);
			TextView tv = (TextView) findViewById(R.id.editTagMore);
			tv.setText(String.valueOf(tags.value.size()) + " tags available");
			listView.setClickable(true);
			listView.setSelectionAfterHeaderView();
			setListAdapter(tagAdapter);
			
		}
		else Info.alert(this, tags.status.toString());
	}

	/**
	 * Launches AddTag activity, allowing admin to add a tag.
	 * Triggered by button press.
	 * 
	 * @param view The current view
	 */
	public void addTag(View view)
	{
		Intent toAddTag = new Intent(this, AddTagActivity.class);
		startActivity(toAddTag);
	}
	
	/**
	 * Launches the EditTag activity with the tag that is selected.
	 * Does not yet call the correct activity, as EditTag does
	 * not yet exist.
	 * 
	 * @param view
	 */
	public void editTag(View view)
	{
		int tagID = (Integer) view.getTag(); 
		Intent toEditTag = new Intent(this, ViewItemActivity.class);
		toEditTag.putExtra("TagID", tagID); 
		startActivity(toEditTag); 
	}

}
